<?php

namespace PHPSpring\Web;

class BaseService {

  protected BaseDao $baseDao;

  public function save ($entity) {
    return $this->baseDao->insert($entity);
  }

  public function saveBatch (array $entities) {
    return $this->baseDao->insertBatch($entities);
  }

  public function saveOrUpdateById ($entity) {
    // TODO
  }

  public function saveOrUpdateBatchById (array $entities) {
    // TODO
  }

  public function remove ($condition) {
    return $this->baseDao->delete($condition);
  }

  public function removeById ($id) {
    return $this->baseDao->deleteById($id);
  }

  public function removeByIds (array $ids) {
    return $this->baseDao->deleteBatchIds($ids);
  }

  public function update ($entity, $condition) {
    return $this->baseDao->update($entity, $condition);
  }

  public function updateById ($entity) {
    return $this->baseDao->updateById($entity);
  }

  public function updateBatchByIds ($entities) {
    if (empty($entities)) {
      return null;
    }
    $num = 0;
    foreach ($entities as $entity) {
      $num += $this->baseDao->updateById($entity);
    }
    return $num;
  }

  public function getById ($id) {
    return $this->baseDao->selectById($id);
  }

  public function getOne ($condition, $orderBy=null) {
    return $this->baseDao->selectOne($condition, $orderBy);
  }

  public function list ($condition, $orderBy=null) {
    return $this->baseDao->selectList($condition, $orderBy);
  }

  public function listByIds (array $ids, $orderBy=null) {
    return $this->baseDao->selectBatchIds($ids, $orderBy);
  }

  public function page ($condition, $page, $pageSize, $orderBy=null) {
    $offset = 0;
    $page = $page - 0;
    if ($page < 1) {
      $page = 1;
    }
    $offset = ($page - 1) * $pageSize;
    return $this->baseDao->selectPage($condition, $offset, $pageSize, $orderBy);
  }

  public function count($condition=null) {
    return $this->baseDao->selectCount($condition);
  }

}